package com.evan.uyclub.OtherImageTool;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.evan.uyclub.Other.Utlis;
import com.evan.uyclub.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sdsmdg.tastytoast.TastyToast;
import com.skydoves.elasticviews.ElasticButton;
import com.tapadoo.alerter.Alerter;

import java.io.FileNotFoundException;

/**
 * 压错方法：
 * <p>
 * 访问：https://m.yisu.com/zixun/213152.html；
 **/

public class YacuoImg extends AppCompatActivity {

    private ElasticButton select_but, keep_but;
    private ImageView compress_img, back;
    private Bitmap bitmap, Image;

    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_yacuoimg);

        //Smart回弹
        SmartRefreshLayout smart = (SmartRefreshLayout) findViewById(R.id.smarts);
        smart.setEnableLoadMore(true);
        smart.setEnablePureScrollMode(true);

        compress_img = findViewById(R.id.compress_img);
        select_but = findViewById(R.id.select_but);
        keep_but = findViewById(R.id.keep_but);
        back = findViewById(R.id.back);
        back.setOnClickListener(view -> {
            this.finish();
        });

        initView();
    }

    public void initView() {
        select_but.setOnClickListener(view -> {
            setSelect();
            //显示动画
//        Animation daoHang = AnimationUtils.loadAnimation(this,R.anim.activity_visible);
//        daoHang.setDuration(1200);
//       keep_but.setVisibility(View.VISIBLE);
//       keep_but.setAnimation(daoHang);
        });
    }

    //打开图片选择器并返回图片URL，onActivityResult函数是专门读取返回的数据的函数，这里返回的URL会自动被获取；
    public void setSelect() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, 1);
    }

    //onActivityResult函数，自动获取所有返回的数据 【protected是本类里有相应的出发时自动执行的】
    //Intent data相当于获取了返回的URL
    @Override
    protected void onActivityResult(int result, int request, Intent data) {
        if (request == RESULT_OK) {
            Uri uri = data.getData();
            ContentResolver contentResolver = this.getContentResolver();
            try {
                bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri));
                compress_img.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        super.onActivityResult(result, request, data);
        //压错并保存
        keep_but.setOnClickListener(view -> {
            //调用压错函数
            Image = Utlis.compressImage(bitmap);
            compress_img.setImageBitmap(Image);
            //调用保存函数
            new Handler().postDelayed(this::Puss, 1500);
        });
    }

    //保存
    public void Puss() {
        //调用保存函数
        Utlis.fileSaveToPublic(YacuoImg.this, "compressIMG", Image);
        TastyToast.makeText(this, "压缩成功喽~", TastyToast.LENGTH_SHORT, TastyToast.SUCCESS).show();
        Alerter.create(this)
                .setTitle("压缩保存成功喽！")
                .setText("已最佳质量为你压缩好了哦~")
                .setDuration(1200)
                .setBackgroundColorRes(R.color.fbutton_color_nephritis)
                .show();
    }
}
